package com.bxd.baodan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.bxd.baodan.R;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;

/**
 * Created by lizhichao on 17/10/27.
 */

public class OAuthActivity extends BaseActivity {
    private TextView content;
    private TextView xieyi;
    private TextView login;
    private String url, platform;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actibity_oauth);
        setBarBack("取消");
        setActionBarTitle("授权登录");
        url = getIntent().getStringExtra("redirect_url");
        platform = getIntent().getStringExtra("platform");


        content = (TextView) findViewById(R.id.content);
        xieyi = (TextView) findViewById(R.id.xieyi);
        login = (TextView) findViewById(R.id.login);
        String account = LoginInfo.getInstance(this).getAccount();
        StringBuffer sb = new StringBuffer();
        sb.append(account.substring(0, 3));
        sb.append("****");
        sb.append(account.substring(7, account.length()));
        account = sb.toString();

        content.setText(Html.fromHtml("将使用保险岛账号" + "<font color='#16c571'>" + account + "</font>" + "登录如意岛享受理财服务。如意岛·安全的综合理财平台。"));

        xieyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = Constant.root_url + "html/common/authprotocol";
                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{url});
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getOauthStatus(1);
            }
        });
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {//点击的是返回键
//            getOauthStatus(0);
//            return false;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    public void getOauthStatus(int oauth) {
        NetUtil netUtil = new NetUtil(OAuthActivity.this, JsonApi.AUTH_DOAUTH);
        netUtil.setParams("from", platform);
        netUtil.setParams("url", url);
        netUtil.setParams("isok",oauth);
        netUtil.postRequest("", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {
                String url =  JSONUtil.getData("redirectUrl", JSONUtil.getData(result));
                Intent intent = new Intent();
                intent.putExtra("url", url);
                OAuthActivity.this.setResult(300,intent);
                finish();
            }

            @Override
            public void onError(String result) {
                super.onError(result);
              finish();

            }
        });

    }


//    public void setBarBack(String title) {
//        findViewById(R.params.bar_iv_back).setVisibility(GONE);
//        TextView tv_back = (TextView) findViewById(R.params.bar_tv_back);
//        tv_back.setText(title);
//        tv_back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                getOauthStatus(0);
//            }
//        });
//    }
}
